package afk.biglog.gui;

import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;

import javax.swing.*;

import afk.biglog.*;

public class FileAdderTransferhandler extends TransferHandler
{

	private final BigLogViewApplication adapter;

	public FileAdderTransferhandler(final BigLogViewApplication mainViewAdapter)
	{
		this.adapter = mainViewAdapter;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public boolean canImport(final TransferSupport support)
	{
		if (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
			return false;

		return true;
	}

	@Override
	public boolean importData(final TransferSupport support)
	{
		if (!canImport(support))
			return false;

		final Transferable t = support.getTransferable();

		try
		{
			@SuppressWarnings("unchecked")
			final List<File> list = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);

			for (final File file : list)
				SwingUtilities.invokeLater(new Runnable()
				{

					@Override
					public void run()
					{
						adapter.openFile(file);
					}
				});
		}
		catch (final UnsupportedFlavorException e)
		{
			return false;
		}
		catch (final IOException e)
		{
			return false;
		}

		return true;
	}
}
